import utilAxios from '@/util/util-axios'
import NtdDataTcpDetail from '@/components/ntd-data/NtdDataTcpDetail'

let serverAppDict = {}

utilAxios.buildDict('function', 'server-app').then(dict => {
    serverAppDict = dict
})

const queryParams = []
const pageConfig = { limit: 20 }
const tableConfig = {}
let extendFormConfig = {}
const formRules = {}

function formOnChange(val, form) {
    extendFormConfig = {}
    if (form['enumDataAcquisitionType'] === 'RULE_PASSIVE') {
        extendFormConfig.__serverAppId = {
            name: '__serverAppId',
            title: '服务应用',
            dictList: serverAppDict
        }
        if (form['enumProtocolType'] === 'HTTP' || form['enumProtocolType'] === 'HTTPS') {
            extendFormConfig.__httpPath = {
                name: '__httpPath',
                title: '访问路径'
            }
            extendFormConfig.__httpType = {
                name: '__httpType',
                title: '请求类型',
                dictList: utilAxios.httpTypeDict
            }
            extendFormConfig.__httpDataType = {
                name: '__httpDataType',
                title: '传参方式',
                dictList: utilAxios.httpDataType
            }
        }
        if (form['enumProtocolType'] === 'TCP') {
            extendFormConfig.__beginTag = {
                name: '__beginTag',
                title: '开始标记'
            }
            extendFormConfig.__endTag = {
                name: '__endTag',
                title: '结束标记'
            }
            extendFormConfig.__rules = {
                name: '__rules',
                title: '转换规则',
                component: NtdDataTcpDetail
            }
        }
        extendFormConfig.__httpResult = {
            name: '__httpResult',
            title: '返回数据'
        }
    }
    if (form['enumDataAcquisitionType'] === 'RULE_ACTIVE') {
        extendFormConfig.__httpHost = {
            name: '__httpHost',
            title: '请求主机'
        }
        extendFormConfig.__httpPort = {
            name: '__httpPort',
            title: '请求端口'
        }
        extendFormConfig.__httpPath = {
            name: '__httpPath',
            title: '请求路径'
        }
        extendFormConfig.__httpLogicParams = {
            name: '__httpLogicParams',
            title: '逻辑参数'
        }
        extendFormConfig.__httpParams = {
            name: '__httpParams',
            title: '请求参数'
        }
        extendFormConfig.__httpHeaders = {
            name: '__httpHeaders',
            title: '请求header'
        }
        extendFormConfig.__httpType = {
            name: '__httpType',
            title: '请求类型',
            dictList: utilAxios.httpTypeDict
        }
        extendFormConfig.__httpDataType = {
            name: '__httpDataType',
            title: '传参方式',
            dictList: utilAxios.httpDataType
        }
        extendFormConfig.__httpCron = {
            name: '__httpCron',
            title: '请求周期',
            type: 18
        }
    }
    return extendFormConfig
}

const formConfig = {
    ...tableConfig,
    enumDataAcquisitionType: {
        formOnChange
    },
    enumProtocolType: {
        formOnChange
    }
}

export async function init(form = {}) {
    return {
        queryParams,
        tableConfig,
        pageConfig,
        formConfig,
        formRules,
        extendFormConfig
    }
}
